package com.hlf.common.factory;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;

/**
 * 获取spring ioc容器所管理bean的工厂
 * <p>
 * 需要配置bean:<bean id="springfactory"
 * class="com.hlf.taika.common.factory.SpringFactory"></bean>
 * 
 * @author shuliangxing
 * 
 * @date 2015-1-14上午11:15:29
 */
public class SpringFactory implements BeanFactoryAware {

    private static BeanFactory beanFactory;

    public void setBeanFactory(BeanFactory factory) throws BeansException {
        SpringFactory.beanFactory = factory;
    }

    /**
     * 根据beanName名字取得bean
     *
     * @param beanName
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanName) {
        if (null != beanFactory) {
            return (T) beanFactory.getBean(beanName);
        }
        return null;
    }

}
